package com.mit.service.core.aop;

import java.util.HashMap;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class DtoTableMap {
	private static Logger logger = Logger.getLogger(DtoTableMap.class);
	private static HashMap map = new HashMap();
	private static final String MAPPING_FILE_PATH = "resources/ibatis/dto-table-mapping.xml";
	static{
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read(Thread.currentThread().getContextClassLoader().getResourceAsStream(MAPPING_FILE_PATH));
			Element root = doc.getRootElement();
			for(Iterator it = root.elementIterator("mapping"); it.hasNext();){
				Element mapping = (Element) it.next();
				map.put(StringUtils.trim(mapping.attributeValue("class"))
						, StringUtils.trim(mapping.attributeValue("table")));
			}
		} catch (DocumentException e) {
			logger.error("Unable to load file: " + MAPPING_FILE_PATH, e);
		}
	}

	public static String getTableName(String className){
		return (String) map.get(className);
	}
}
